package com.tuanzi.loan.web.bean.receipt.statistics;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.receipt.product.ReceiptProductInfo;
import com.tuanzi.loan.business.entity.system.Dictionary;
import com.tuanzi.loan.business.service.receipt.product.ReceiptProductInfoService;
import com.tuanzi.loan.business.service.receipt.statistics.ReceiptRechargeStatisticsService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.receipt.statistics.ReceiptRechargeStatisticsSearchRequest;
import com.tuanzi.loan.vo.response.receipt.statistics.ReceiptRechargeStatisticsSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.receipt.statistics.ReceiptRechargeStatisticsLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

/**
 * 每日统计
 * @author zzw
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ReceiptRechargeStatisticsBean extends JSFListingBean<ReceiptRechargeStatisticsSearchRequest, ReceiptRechargeStatisticsSearchResponse> {

    private static final long serialVersionUID = -7052849653608623497L;

    @Autowired
    private ReceiptProductInfoService receiptProductInfoService;

    @Autowired
    private ReceiptRechargeStatisticsService rechargeStatisticsService;

    private List<ReceiptProductInfo> receiptProductInfos;


    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    public void search() {
        if($.getStartDate() != null && $.getEndDate() != null && $.getStartDate().after($.getEndDate())) {
            addErrorMessage("开始日期不能比结束日期大");
            return;
        }
    }

    public void onReset() {
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new ReceiptRechargeStatisticsSearchRequest();
        $.setUserId(CurrentUser.get().getUserId());
        lazyDataModel = new ReceiptRechargeStatisticsLazyDataModel($);
        receiptProductInfos = receiptProductInfoService.findAllProductIdAndName();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.RECEIPT_ORDER_SOURCE_FROM);
    }
}
